import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store';

Vue.use(VueRouter);

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: () => import('@/views/layout/index'),
    redirect: '/dashboard',
    meta: { requireAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', requireAuth: true }
      },
      {
        path: 'courses',
        name: 'Courses',
        component: () => import('@/views/courses/index'),
        meta: { title: '我的课程', requireAuth: true }
      },
      {
        path: 'course/create',
        name: 'CourseCreate',
        component: () => import('@/views/courses/edit'),
        meta: { title: '创建课程', requireAuth: true }
      },
      {
        path: 'course/edit/:id',
        name: 'CourseEdit',
        component: () => import('@/views/courses/edit'),
        meta: { title: '编辑课程', requireAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/profile/index'),
        meta: { title: '个人中心', requireAuth: true }
      },
      {
        path: 'schools',
        name: 'Schools',
        component: () => import('@/views/schools/index'),
        meta: { title: '学校管理', requireAuth: true, roles: [2] } // 仅管理员可访问
      },
      {
        path: 'school/create',
        name: 'SchoolCreate',
        component: () => import('@/views/schools/edit'),
        meta: { title: '创建学校', requireAuth: true, roles: [2] }
      },
      {
        path: 'school/edit/:id',
        name: 'SchoolEdit',
        component: () => import('@/views/schools/edit'),
        meta: { title: '编辑学校', requireAuth: true, roles: [2] }
      },
      {
        path: 'teachers',
        name: 'Teachers',
        component: () => import('@/views/teachers/index'),
        meta: { title: '教师管理', requireAuth: true, roles: [2] } // 仅管理员可访问
      },
      {
        path: 'teacher/create',
        name: 'TeacherCreate',
        component: () => import('@/views/teachers/edit'),
        meta: { title: '添加教师', requireAuth: true, roles: [2] }
      },
      {
        path: 'teacher/edit/:id',
        name: 'TeacherEdit',
        component: () => import('@/views/teachers/edit'),
        meta: { title: '编辑教师', requireAuth: true, roles: [2] }
      },
      {
        path: 'subjects',
        name: 'Subjects',
        component: () => import('@/views/subjects/index'),
        meta: { title: '学科管理', requireAuth: true, roles: [2] } // 仅管理员可访问
      },
      {
        path: 'subject/create',
        name: 'SubjectCreate',
        component: () => import('@/views/subjects/create'),
        meta: { title: '添加学科', requireAuth: true, roles: [2] }
      },
      {
        path: 'subject/edit/:id',
        name: 'SubjectEdit',
        component: () => import('@/views/subjects/edit'),
        meta: { title: '编辑学科', requireAuth: true, roles: [2] }
      },
      {
        path: 'materials',
        name: 'Materials',
        component: () => import('@/views/materials/index'),
        meta: { title: '课件笔记', requireAuth: true }
      },
      {
        path: 'material/create',
        name: 'MaterialCreate',
        component: () => import('@/views/materials/edit'),
        meta: { title: '添加课件笔记', requireAuth: true }
      },
      {
        path: 'material/edit/:id',
        name: 'MaterialEdit',
        component: () => import('@/views/materials/edit'),
        meta: { title: '编辑课件笔记', requireAuth: true }
      },
      {
        path: 'material/detail/:id',
        name: 'MaterialDetail',
        component: () => import('@/views/materials/detail'),
        meta: { title: '课件笔记详情', requireAuth: true }
      },
      // PPT课件中心路由
      {
        path: 'ppt-center',
        name: 'PPTCenter',
        component: () => import('@/views/ppt-center/index'),
        meta: { title: 'PPT课件中心', requireAuth: true }
      },
      {
        path: 'ppt-center/detail/:id',
        name: 'PPTDetail',
        component: () => import('@/views/ppt-center/detail'),
        meta: { title: 'PPT课件详情', requireAuth: true }
      },
      {
        path: 'ppt-center/upload',
        name: 'PPTUpload',
        component: () => import('@/views/ppt-center/upload'),
        meta: { title: '上传PPT课件', requireAuth: true, roles: [1, 2] } // 仅教师和管理员可上传
      },
      // AI生成课件/笔记路由
      {
        path: 'ai-material-generate',
        name: 'AIMaterialGenerate',
        component: () => import('@/views/materials/ai-generate'),
        meta: { title: '智能生成课件/笔记', requireAuth: true }
      },
      // 试题管理路由
      {
        path: 'questions',
        name: 'Questions',
        component: () => import('@/views/questions/index'),
        meta: { title: '试题管理', requireAuth: true, roles: [1, 2] } // 教师和管理员可访问
      },
      {
        path: 'question/create',
        name: 'QuestionCreate',
        component: () => import('@/views/questions/edit'),
        meta: { title: '添加试题', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'question/edit/:id',
        name: 'QuestionEdit',
        component: () => import('@/views/questions/edit'),
        meta: { title: '编辑试题', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'question/detail/:id',
        name: 'QuestionDetail',
        component: () => import('@/views/questions/detail'),
        meta: { title: '试题详情', requireAuth: true, roles: [1, 2] }
      },
      // 试卷管理路由
      {
        path: 'papers',
        name: 'Papers',
        component: () => import('@/views/papers/index'),
        meta: { title: '试卷管理', requireAuth: true, roles: [1, 2] } // 教师和管理员可访问
      },
      {
        path: 'ai-paper',
        name: 'AIPaperGenerate',
        component: () => import('@/views/papers/ai-generate'),
        meta: { title: '智能生成试卷', requireAuth: true, roles: [1, 2] } // 教师和管理员可访问
      },
      {
        path: 'paper/create',
        name: 'PaperCreate',
        component: () => import('@/views/papers/edit'),
        meta: { title: '创建试卷', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'paper/edit/:id',
        name: 'PaperEdit',
        component: () => import('@/views/papers/edit'),
        meta: { title: '编辑试卷', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'paper/detail/:id',
        name: 'PaperDetail',
        component: () => import('@/views/papers/detail'),
        meta: { title: '试卷详情', requireAuth: true, roles: [1, 2] }
      },
      // 考试管理路由
      {
        path: 'exams',
        name: 'Exams',
        component: () => import('@/views/exams/index'),
        meta: { title: '考试管理', requireAuth: true, roles: [1, 2] } // 教师和管理员可访问
      },
      {
        path: 'exam/create',
        name: 'ExamCreate',
        component: () => import('@/views/exams/edit'),
        meta: { title: '创建考试', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'exam/edit/:id',
        name: 'ExamEdit',
        component: () => import('@/views/exams/edit'),
        meta: { title: '编辑考试', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'exam/detail/:id',
        name: 'ExamDetail',
        component: () => import('@/views/exams/detail'),
        meta: { title: '考试详情', requireAuth: true }
      },
      {
        path: 'exams/results',
        name: 'ExamResults',
        component: () => import('@/views/exams/results'),
        meta: { title: '考试成绩', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'exam/result/:id',
        name: 'ExamResultDetail',
        component: () => import('@/views/exams/result-detail'),
        meta: { title: '考试成绩详情', requireAuth: true }
      },
      // 学生考试路由
      {
        path: 'student/exams',
        name: 'StudentExams',
        component: () => import('@/views/student-exams/index'),
        meta: { title: '我的考试', requireAuth: true, roles: [0] } // 学生可访问
      },
      {
        path: 'student/exam/:id',
        name: 'StudentExamDetail',
        component: () => import('@/views/student-exams/detail'),
        meta: { title: '考试详情', requireAuth: true, roles: [0] }
      },
      {
        path: 'student/exam/take/:id',
        name: 'TakeExam',
        component: () => import('@/views/student-exams/take'),
        meta: { title: '参加考试', requireAuth: true, roles: [0] }
      },
      {
        path: 'student/exam/result/:id',
        name: 'ExamResult',
        component: () => import('@/views/student-exams/result'),
        meta: { title: '考试结果', requireAuth: true, roles: [0] }
      },
      // 学生管理路由
      {
        path: 'students',
        name: 'Students',
        component: () => import('@/views/students/index'),
        meta: { title: '学生管理', requireAuth: true, roles: [1, 2] } // 教师和管理员可访问
      },
      {
        path: 'student/create',
        name: 'StudentCreate',
        component: () => import('@/views/students/edit'),
        meta: { title: '添加学生', requireAuth: true, roles: [1, 2] }
      },
      {
        path: 'student/edit/:id',
        name: 'StudentEdit',
        component: () => import('@/views/students/edit'),
        meta: { title: '编辑学生', requireAuth: true, roles: [1, 2] }
      }
    ]
  },
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    meta: { title: '404' }
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? to.meta.title + ' - 在线教育平台' : '在线教育平台';
  
  // 判断该路由是否需要登录权限
  if (to.matched.some(record => record.meta.requireAuth)) {
    // 判断是否已登录
    if (store.getters.token) {
      // 判断角色权限
      if (to.meta.roles && to.meta.roles.length > 0) {
        const userRole = store.getters.userInfo.role;
        if (to.meta.roles.includes(userRole)) {
          next();
        } else {
          next('/dashboard');
        }
      } else if (to.path !== '/login' && !store.getters.isTeacher && !store.getters.isAdmin) {
        next({
          path: '/login',
          query: { redirect: to.fullPath }
        });
      } else {
        next();
      }
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    }
  } else {
    next();
  }
});

export default router; 